from appium.webdriver.common.mobileby import MobileBy
from selenium.common.exceptions import NoSuchElementException

from app.addmember_page import AddMemberPage
from app.base_page import BasePage
from app.personalinfo_page import PersonalInfo


class ContractPage(BasePage):
    def goto_addmember(self):
        # 滑动查找添加成员
        self.swipe_find(3,"添加成员").click()
        return AddMemberPage(self.driver)

    def goto_memberinfo(self):
        # 找到添加联系人的layout位置，index=2时，说明没有联系人
        ele = self.find(MobileBy.XPATH,"//*[@text='添加成员']/../../../../..[@resource-id='com.tencent.wework:id/dqz']")
        index = int(ele.get_attribute("text"))
        if index == 2:
            raise NoSuchElementException("当前目录没有微信联系人，请添加联系人后再操作")
        else:
            # 点击第一个成员进入到个人信息页面
            self.find_and_click(MobileBy.XPATH,"//*[@text='添加成员']/../../../../..[@resource-id='com.tencent.wework:id/dqz']/..[@resource-id='com.tencent.wework:id/b00']/android.widget.RelativeLayout[3]//android.view.ViewGroup")
            return PersonalInfo(self.driver)

    # 获取通讯录页面信息
    def get_contract_message(self):
        return self.get_page_source()